/**
 * 作者:李杰
 * email: maillijie@gmail.com
 */
package com.javainfo.actions.forums;

import java.util.HashMap;
import java.util.Map;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.apache.struts2.rest.DefaultHttpHeaders;
import org.apache.struts2.rest.HttpHeaders;

import com.javainfo.dto.PageDto;
import com.javainfo.entity.Article;
import com.javainfo.entity.ArticleCategory;
import com.javainfo.entity.User;
import com.javainfo.logic.CompetenceComponent;
import com.javainfo.simpledto.ArticleDto;
import com.javainfo.simpledto.SubArticleDto;
import com.javainfo.util.Const;
import com.javainfo.util.JavaInfoActionSupport;

@Results( { @Result(name = "success", type = "redirectAction", params = {
		"actionName", "category" }) })
public class ArticleCtrl extends JavaInfoActionSupport{

	/** serialVersionUID */
	private static final long serialVersionUID = 1L;

	@Override
	public String index() {
		return INDEX;
	}

	@Override
	public HttpHeaders show() {
	
		//更新帖子的点击数
		getServiceFactory().getArticleService().updateClickNo(getSession(),
				Integer.valueOf(getId()));
		//获取帖子的详细信息
		ArticleDto articleDto = getServiceFactory().getArticleService()
				.getArticleInfo(Integer.valueOf(getId()));
		
		//设置分页显示的功能
		setPageDto(PageDto.init(articleDto.getSubArticleList().size(),getPageDto()));
		articleDto.setSubArticleList(articleDto.getSubArticleList()
				.subList(getPageDto().getStartRow(), getPageDto().getEndRow()));
		
		setModel(articleDto);
		return new DefaultHttpHeaders(SHOW);
	}
	
	@Override
	public HttpHeaders create() {
		
		//获取画面输入的值，转换成service层需要的Dto
		String categoryId = getRequest().getParameter("categoryId");
		String subCategoryId = getRequest().getParameter("subCategoryId");
		String title = getRequest().getParameter("title");
		String content = getRequest().getParameter("content");
		
		ArticleDto articleDto = new ArticleDto();
		articleDto.setCategoryId(categoryId);
		articleDto.setContent(content);
		articleDto.setSubCategoryId(subCategoryId);
		articleDto.setTitle(title);
		
		//判断用户是否有新建帖子的权限并且新建帖子
		Integer articleId = null;
		Object userTemp = getSession().get(Const.LOGIN_INFO);
		if (userTemp == null
				|| !CompetenceComponent.canCreateArticle((User) userTemp)) {
			//throw new AppException("用户没有登录或者没有权限!");
			return new DefaultHttpHeaders(Const.NO_LOGIN);
		} else {
			articleId = getServiceFactory().getArticleService().saveNewArticle(
					(User) userTemp, articleDto, this);
		}
		
		//转向帖子内容显示
		setId(String.valueOf(articleId));
		return this.show();
	}

	/**
	 * 创建新回复
	 * @return
	 */
	public HttpHeaders createSubArticle() {
		
		//获取画面输入的值，转换成service层需要的Dto
		String subArticleTitle = getRequest().getParameter("subArticleTitle");
		String articleId = getRequest().getParameter("articleId");
		String subArticleContent = getRequest().getParameter("subArticleContent");
		
		SubArticleDto subArticleDto = new SubArticleDto();
		subArticleDto.setContent(subArticleContent);
		subArticleDto.setTitle(subArticleTitle);
		subArticleDto.setArticleId(Integer.valueOf(articleId));
		
		//判断用户是否能够回复，并且创建回复
		Object userTemp = getSession().get(Const.LOGIN_INFO);
		if (userTemp == null
				|| !CompetenceComponent.canCreateSubArticle((User) userTemp)) {
			//throw new AppException("用户没有登录或者没有权限!");
			return new DefaultHttpHeaders(Const.NO_LOGIN);
		} else {
			subArticleDto.setUserId(((User)userTemp).getId());
			getServiceFactory().getSubArticleService().saveSubArticle(subArticleDto);
		}
		//转向帖子内容显示
		setId(articleId);
		return this.show();
	}
	

	/* (non-Javadoc)
	 * @see com.javainfo.util.JavaInfoActionSupport#edit()
	 */
	public HttpHeaders edit() {
		Article article = getServiceFactory().getArticleService().getById(Integer.valueOf(getId()));
		ArticleCategory articleCategory = article.getCategory();
		Map<String, Object> model = new HashMap<String, Object>();
		model.put("category", articleCategory);
		model.put("article", article);
		model.put("articleContent", article.getContent());
		setModel(model);
		return new DefaultHttpHeaders(EDIT);
	}


	/* (non-Javadoc)
	 * @see com.javainfo.util.JavaInfoActionSupport#update()
	 */
	public HttpHeaders update() {
		
		//获取画面输入的值，转换成service层需要的Dto
		String articleTitle = getRequest().getParameter("articleTitle");
		String categoryId = getRequest().getParameter("categoryId");
		String articleId = getRequest().getParameter("articleId");
		String subCategoryId = getRequest().getParameter("subCategoryId");
		String articleContent = getRequest().getParameter("articleContent");
		
		ArticleDto articleDto = new ArticleDto();
		articleDto.setCategoryId(categoryId);
		articleDto.setTitle(articleTitle);
		articleDto.setId(Integer.valueOf(articleId));
		articleDto.setSubCategoryId(subCategoryId);
		articleDto.setContent(articleContent);
		
		//判断用户是否能够更新，并且更新
		Object userTemp = getSession().get(Const.LOGIN_INFO);
		if (userTemp == null) {
			return new DefaultHttpHeaders(Const.NO_LOGIN);
		} else {
			getServiceFactory().getArticleService().updateArticle((User)userTemp, articleDto);
		}
		//转向帖子内容显示
		setId(articleId);
		return this.show();
	}

	/* (non-Javadoc)
	 * @see com.javainfo.util.JavaInfoActionSupport#destroy()
	 */
	public HttpHeaders destroy() {
		Article article = getServiceFactory().getArticleService().getById(Integer.valueOf(getId()));
		getServiceFactory().getArticleService().delete(article);
		return new DefaultHttpHeaders(SUCCESS);
	}	
}
